<?php
use Carbon\Carbon;

class AdminApartmentController extends BaseController {

    use HandleImagesTrait; // traits para addImagesToEntidad()
    
//    public function __construct()
//    {
//        // Para formatear la fecha en español
//        setlocale(LC_TIME, 'Spanish');
//    }
//
    /**
     * Display a listing of the resource.
     *
     * @return Response
     */
    public function index() {
        $apartments = Apartment::orderBy('fecha', 'desc')->get();
        return View::make('admin.apartments.index', ['apartments' => $apartments]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return Response
     */
    public function create() {
        return View::make('admin.apartments.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @return Response
     */
    public function store() {
        // validate
        $validatorApa = Apartment::validate(Input::all());
        
        // validate imagenes
        foreach (Input::file('files') as $file) {
            $validatorImg = Imageapa::validate(['file' => $file]);
            if ($validatorImg->fails()) {
                return Redirect::back()->withErrors($validatorImg)->withInput();
            }
        }

        // process the form
        if ($validatorApa->fails()) {
              return Redirect::back()->withErrors($validatorApa)->withInput();
        } else {
            // store
            $apartment = new Apartment;
            $apartment->lugar = Input::get('lugar');
            $apartment->texto = Input::get('texto');
            $apartment->estado = Input::get('estado');
            $apartment->fecha = Carbon::create(Input::get('anio'), Input::get('mes'), 1); 
            
            $apartment->save();
            
            //Image managing
            if(Input::hasFile('files')){
                $this->addImagesToEntidad(Input::file('files'), $apartment, 'apartment_id', 'imageapa_apartment', 'Imageapa');
            }
            // redirect
            Session::flash('message', 'El nuevo apartment fue creado');
            return Redirect::to('admin/apartments');
        }        
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return Response
     */
    public function show($id) {
        $apartment = Apartment::with('imagesOrdered')
            ->find($id);

        if( is_null($apartment)) {
            App::abort(404);
        }

        $images = $apartment->imagesOrdered;

        $otrosApartments = Apartment::active()
            ->where('id', '<>', $apartment->id)
            ->orderBy('fecha', 'desc')
            ->get();

        return View::make('admin.apartments.show', compact('apartment', 'images', 'otrosApartments'));

//        $apartment = Apartment::find($id);
//        return View::make('admin.apartments.show', ['apartment' => $apartment]);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return Response
     */
    public function edit($id) {
        $apartment = Apartment::find($id);
        $images = $apartment->imagesOrdered;
        $entidad = 'apartment';
        return View::make('admin.apartments.edit', ['apartment' => $apartment, 'images' => $images, 'entidad' => $entidad]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  int  $id
     * @return Response
     */
    
    // This controller method will process the edit form. 
    // It is very similar to store(). We will validate, update, and redirect.
    
    public function update($id) {
        $validatorApa = Apartment::validate(Input::all());
        
        // validate imagenes
        foreach (Input::file('files') as $file) {
            $validatorImg = Imageapa::validate(['file' => $file]);
            if ($validatorImg->fails()) {
                return Redirect::back()->withErrors($validatorImg)->withInput();
            }
        }
        if ($validatorApa->fails()) {
            return Redirect::back()->withErrors($validatorApa)->withInput();
        } 
        else {
            // store
            $apartment = Apartment::find($id);
            $apartment->lugar = Input::get('lugar');
            $apartment->texto = Input::get('texto');
            $apartment->estado = Input::get('estado');
            $apartment->fecha = Carbon::create(Input::get('anio'), Input::get('mes'), 1); 
            
            $apartment->save();

            //Image managing
            if(Input::hasFile('files')){
                $this->addImagesToEntidad(Input::file('files'), $apartment, 'apartment_id', 'imageapa_apartment', 'Imageapa');
            }

            // redirect
            Session::flash('message', 'Apartment modificado');
            return Redirect::to('admin/apartments');
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return Response
     */
    public function destroy($id) {
        // delete
        $apartment = Apartment::find($id);
        
        DB::transaction(function($apartment) use ($apartment) {
            
            foreach($apartment->images as $img){
                $img->delete();
            }
            $apartment->delete();
        });
        
        // redirect
        Session::flash('message', 'Eliminación exitosa');
        return Redirect::to('admin/apartments');
    }
}
